package com.glory.huaweipda.view

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import com.glory.huaweipda.R
import com.glory.huaweipda.base.MyApp
import com.glory.huaweipda.bean.AreaEqpInfoBean
import com.glory.huaweipda.databinding.ActivityEquipmentBinding
import com.glory.huaweipda.viewmodel.EquipmentModel
import com.glory.mvvmnet.base.BaseActivity
import com.google.android.material.tabs.TabLayout
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import kotlinx.android.synthetic.main.activity_equipment.*


class EquipmentActivity : BaseActivity<EquipmentModel>() {

    var type: String = ""
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    type = "oo"
                    val intent = Intent()
                    intent.putExtra("showPlugin", "errorLog")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
                    intent.setClass(this@EquipmentActivity, clazz)
                    startActivity(intent)
                }
            }
        }
    }

    override fun layoutId(): Int {
        return R.layout.activity_equipment
    }


    override fun initView(savedInstanceState: Bundle?) {
        val instance = MyApp.instance
        instance!!.handler = handler
        val binding: ActivityEquipmentBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_equipment)

        val area =
            FreeApi_SharePreferencesUtils.getSharePre("SystemSet", "area", applicationContext)
        areaText.text = area
        binding.equipmentModel = viewModel
        viewModel.defaultGetAreaEqpInfo(area)
        viewModel.initTab()
//        viewModel.defUI.callObserve.observe(this) {
//            when (it.type) {
//                viewModel.EQUIPMENT -> {
//
//
//
//                }
//            }
//        }
        ivBack.setOnClickListener{
            finish()
        }
        for (i in 0..viewModel.titles.size-1) {
            tab_layout.addTab(tab_layout.newTab().setText(viewModel.titles.get(i)));
        }
        tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val text = tab!!.text.toString()
                viewModel.initAssets(text)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })

        viewModel.defUI.callObserve.observe(this) {
            if ("oo".equals(it.type))it.type = -1
            when (it.type) {
                1 -> {
                    if ("com.glory.huaweipda.view.SkipDialogActivity".equals(getTopActivity(applicationContext))){
                        SkipDialogActivity.getActivity()
                    }
                    val lotsBean = it.item as ArrayList<AreaEqpInfoBean>
                    viewModel.areaEqpInfoBean.addAll(lotsBean)
                    viewModel.initAssets("全部")
                }

                2->{
                    if (viewModel.mfgItems.size!=0){
                        rvMFGList.visibility=View.VISIBLE
                        rlItem.visibility=View.GONE
                    } else {
                        rvMFGList.visibility=View.GONE
                        rlItem.visibility=View.VISIBLE
                    }
                }
            }
        }

    }

    override fun initData() {

    }

    fun getTopActivity(context: Context): String {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val cn = am.getRunningTasks(1)[0].topActivity
        Log.d("Chunna.zheng", "cls:" + cn!!.className) //包名加类名
        return cn.className
    }

}
